home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / 68kdis.zip / MAIN.C < prev    next >
C/C++ Source or Header  |  1988-12-03  |  6KB  |  309 lines

  1. /*
  2.  *    SCCS:    @(#)main.c    1.2    11/2/84    14:19:31
  3.  *    Main routine etc.
  4.  *
  5.  ***********************************************************************
  6.  *    This software is copyright of
  7.  *
  8.  *        John M Collins
  9.  *        47 Cedarwood Drive
  10.  *        St Albans
  11.  *        Herts, AL4 0DN
  12.  *        England            +44 727 57267
  13.  *
  14.  *    and is released into the public domain on the following conditions:
  15.  *
  16.  *        1.  No free maintenance will be guaranteed.
  17.  *        2.  Nothing may be based on this software without
  18.  *            acknowledgement, including incorporation of this
  19.  *            notice.
  20.  *
  21.  *    Notwithstanding the above, the author welcomes correspondence and bug
  22.  *    fixes.
  23.  ***********************************************************************
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <fcntl.h>
  28. #include <a.out.h>
  29. #include <ldfcn.h>
  30. #include "unc.h"
  31.  
  32. #define    LINELNG    70
  33.  
  34. void    inturdat(), intutext(), intudat(), intlsym();
  35. void    ptext(), pdata(), pabs(), pbss(), lscan();
  36.  
  37. ef_fids    mainfile;
  38.  
  39. int    nmods;            /*  Number of modules it looks like  */
  40.  
  41. char    *tfnam = "split";
  42.  
  43. char    lsyms;            /*  Generate local symbols  */
  44. char    verbose;        /*  Tell the world what we are doing  */
  45. char    noabs;            /*  No non-global absolutes  */
  46. int    rel;            /*  File being analysed is relocatable  */
  47. int    lpos;
  48. char    shlibout;        /*  output values for shlib constants */
  49.  
  50. symbol    dosymb();
  51. struct    libit    *getfnam();
  52.  
  53. /*
  54.  *    Get hex characters, also allowing for 'k' and 'm'.
  55.  */
  56.  
  57. int    ghex(str)
  58. register  char    *str;
  59. {
  60.     register  int    result = 0;
  61.     register  int    lt;
  62.  
  63.     for  (;;)  {
  64.         lt = *str++;
  65.         switch  (lt)  {
  66.         default:
  67. err:            (void) fprintf(stderr, "Invalid hex digit \'%c\'\n", lt);
  68.             exit(1);
  69.             
  70.         case '\0':
  71.             return    result;
  72.             
  73.         case '0':case '1':case '2':case '3':case '4':
  74.         case '5':case '6':case '7':case '8':case '9':
  75.             result = (result << 4) + lt - '0';
  76.             continue;
  77.             
  78.         case 'a':case 'b':case 'c':case 'd':case 'e':case 'f':
  79.             result = (result << 4) + lt - 'a' + 10;
  80.             continue;
  81.  
  82.         case 'A':case 'B':case 'C':case 'D':case 'E':case 'F':
  83.             result = (result << 4) + lt - 'A' + 10;
  84.             continue;
  85.         
  86.         case 'k':case 'K':
  87.             if  (*str != '\0')
  88.                 goto  err;
  89.             return  result << 10;
  90.             
  91.         case 'm':case 'M':
  92.             if  (*str != '\0')
  93.                 goto  err;
  94.             return  result << 20;
  95.         }
  96.     }
  97. }
  98.  
  99. /*
  100.  *    Process entry line options.  Return number dealt with.
  101.  */
  102.  
  103. int    doopts(av)
  104. char    *av[];
  105. {
  106.     register  int    cnt = 0, lt;
  107.     register  char    *arg;
  108.     
  109.     for  (;;)  {
  110.         arg = *++av;
  111.         if  (*arg++ != '-')
  112.             return    cnt;
  113.         cnt++;
  114.         
  115. nx:        switch  (lt = *arg++)  {
  116.         default:
  117.             (void) fprintf(stderr, "Bad option -%c\n", lt);
  118.             exit(1);
  119.             
  120.         case  '\0':
  121.             continue;
  122.             
  123.         case  'l':    /*  A file name  */
  124.         case  'L':
  125.             return    cnt - 1;
  126.             
  127.         case  's':
  128.             lsyms++;
  129.             goto  nx;
  130.             
  131.         case  'v':
  132.             verbose++;
  133.             goto  nx;
  134.  
  135.         case  'V':
  136.             shlibout++;
  137.             goto  nx;
  138.  
  139.         case  'a':
  140.             noabs++;
  141.             goto  nx;
  142.  
  143.         case  't':
  144.             if  (*arg == '\0')  {
  145.                 cnt++;
  146.                 arg = *++av;
  147.                 if  (arg == NULL) {
  148. bo:                    (void) fprintf(stderr,"Bad -%c option\n",lt);
  149.                     exit(1);
  150.                        }
  151.             }
  152.             tfnam = arg;
  153.             continue;
  154.             
  155.         case  'o':
  156.             if  (*arg == '\0')  {
  157.                 cnt++;
  158.                 arg = *++av;
  159.                 if  (arg == NULL)
  160.                     goto  bo;
  161.             }
  162.             if  (freopen(arg, "w", stdout) == NULL)  {
  163.                 (void) fprintf(stderr, "Help! cant open %s\n", arg);
  164.                 exit(20);
  165.             }
  166.             continue;
  167.         }
  168.     }
  169. }
  170.     
  171. /*
  172.  *    Open binary files.  Arrange to erase them when finished.
  173.  */
  174.  
  175. void    bfopen(nam, fid)
  176. char    *nam;
  177. ef_fid    fid;
  178. {
  179.     char    fnam[80];
  180.     
  181.     (void) sprintf(fnam, "%s.tx", nam);
  182.     if  ((fid->ef_t = open(fnam, O_RDWR|O_CREAT, 0666)) < 0)  {
  183. efil:        (void) fprintf(stderr, "Help could not open %s\n", fnam);
  184.         exit(4);
  185.     }
  186.     (void) unlink(fnam);
  187.     (void) sprintf(fnam, "%s.dt", nam);
  188.     if  ((fid->ef_d = open(fnam, O_RDWR|O_CREAT, 0666)) < 0)
  189.         goto  efil;
  190.     (void) unlink(fnam);
  191. }
  192.  
  193. /*
  194.  *    Close binary files.  They should get zapped anyway.
  195.  */
  196.  
  197. void    bfclose(fid)
  198. ef_fid    fid;
  199. {
  200.     (void) close(fid->ef_t);
  201.     (void) close(fid->ef_d);
  202. }
  203.  
  204. /*
  205.  *    Main routine.
  206.  */
  207.  
  208. main(argc, argv)
  209. int    argc;
  210. char    *argv[];
  211. {
  212.     int    i;
  213.     char    *progname = argv[0];
  214.     char    *msg;
  215.     register  struct  libit  *lfd;
  216.     
  217.     setbuf(stdout,NULL);
  218.     setbuf(stderr,NULL);
  219.  
  220.     i = doopts(argv);
  221.     argc -= i;
  222.     argv += i;
  223.     
  224.     if  (argc < 2)  {
  225.         (void) fprintf(stderr, "Usage: %s [ options ] file\n", progname);
  226.         exit(1);
  227.     }
  228.     
  229.     lfd = getfnam(argv[1]);
  230.     if  (TYPE(lfd->ldptr) == ARTYPE)  {
  231.         (void) fprintf(stderr, "Main file (%s) cannot be library\n", argv[1]);
  232.         exit(2);
  233.     }
  234.     
  235.     bfopen(tfnam, &mainfile);
  236.     if  (verbose)
  237.         (void) fprintf(stderr, "Scanning text\n");
  238.     if  (!rtext(lfd->ldptr, &mainfile))  {
  239.         msg = "text";
  240. bf:        (void) fprintf(stderr, "Bad format input file - reading %s\n", msg);
  241.         exit(5);
  242.     }
  243.     if  (verbose)
  244.         (void) fprintf(stderr, "Scanning data\n");
  245.     if  (!rdata(lfd->ldptr, &mainfile))  {
  246.         msg = "data";
  247.         goto  bf;
  248.     }
  249.     if  (verbose)
  250.         (void) fprintf(stderr, "Scanning symbols\n");
  251.     if  (!rsymb(lfd->ldptr, dosymb, &mainfile))  {
  252.         msg = "symbols";
  253.         goto  bf;
  254.     }
  255.     if  (verbose)
  256.         (void) fprintf(stderr, "Scanning for relocation\n");
  257.     if  ((rel = rrel(lfd->ldptr, lfd->ldptr2, &mainfile)) < 0)  {
  258.         msg = "reloc";
  259.         goto  bf;
  260.     }
  261.     
  262.     if  (rel)  {
  263.         if  (verbose)
  264.             (void) fprintf(stderr, "File is relocatable\n");
  265.         if  (argc > 2)
  266.             (void) fprintf(stderr, "Sorry - no scan on reloc files\n");
  267.     }
  268.     else
  269.         lscan(argc - 2, &argv[2]);
  270.  
  271.     if  (verbose)
  272.         (void) fprintf(stderr, "End of input\n");
  273.     
  274.     ldaclose(lfd->ldptr2);
  275.     ldclose(lfd->ldptr);
  276.     if  (nmods > 0)
  277.         (void) fprintf(stderr, "Warning: at least %d merged modules\n",
  278.             nmods + 1);
  279.  
  280.     if  (mainfile.ef_stvec != NULL)  {
  281.         free(mainfile.ef_stvec);
  282.         mainfile.ef_stvec = NULL;
  283.         mainfile.ef_stcnt = 0;
  284.     }
  285.     
  286.     if  (verbose)
  287.         (void) fprintf(stderr, "Text anal 1\n");
  288.     intutext();
  289.     if  (verbose)
  290.         (void) fprintf(stderr, "Data anal 1\n");
  291.     intudat(&mainfile);
  292.     if  (!rel)  {
  293.         if  (verbose)
  294.             (void) fprintf(stderr, "Data anal 2\n");
  295.         inturdat(&mainfile);
  296.     }
  297.     if  (lsyms)  {
  298.         if  (verbose)
  299.             (void) fprintf(stderr, "Local symbol scan\n");
  300.         intlsym();
  301.     }
  302.     pabs();
  303.     ptext(&mainfile);
  304.     pdata(&mainfile);
  305.     pbss(&mainfile);
  306.     bfclose(&mainfile);
  307.     exit(0);
  308. }
  309.